// pages/square/square.ts
import {get,patch} from "@utils/http"
import dayjs from "dayjs"

Component({

  /**
   * 页面的初始数据
   */
  data: {
    form:{
      searchkey:"",
      type:"推荐",
      page:0,
      size:5,
      selectSchool:"广东工业大学",
      selectRegion:"大学城",
    },
    shopTypes:["推荐","娱乐","丽人","生活"],
    showSelectRegion:false,
    schoolRegions:[{values:["1","2","3"]},{values:["1","2","3"]}],
    colors:["blue","yellow","lightblue"],
    shopList:[],
    total:0,
    pulldownStatus:false,
    pulldownTimeout:0,
    scrollViewHeight:0,
    reachButtom:false,
    last:false,
  },
  methods:{
    getSearchKey(e){
      const form=this.data.form
      form.searchkey=e.detail.trim()
      form.page=0
      form.size=5
      this.setData({
        form,
        shopList:[]
      })
    },
    getType(e){
      this.setData({
        form:{
          ...this.data.form,
          type:e.detail.title,
          page:0,
          size:5,
        },
        shopList:[]
      })
      this.getshop()
    },
    closeSelection(){
      this.setData({
        showSelectRegion:false
      })
    },
    handleShowSelection(){
      this.setData({
        showSelectRegion:true
      })
    },
    changeSchoolRegion:function(e:any){
      const{value}=e.detail
      this.setData({
        form:{
          ...this.data.form,
          selectSchool:value[0],
          selectRegion:value[1]
        }
      })
    },
    async getshop(){
     const res=await get("/shop",{
        flag:0,
        pass:1,
        school:this.data.form.selectSchool,
        zone:this.data.form.selectRegion,
        type:this.data.form.type,
        page:this.data.form.page,
        size:this.data.form.size,
        name:this.data.form.searchkey
      })
      const shopList=[]
      for (let i = 0; i < res.content.length; i++) {
        const item = res.content[i];
        const shop={
          id:item[0],
          userId:item[1],
          school:item[2],
          zone:item[3],
          name:item[4],
          rank:item[5]?item[5].toFixed(1):0,
          type:item[6],
          status:item[8],
          location:item[9],
          conditions:item[7].split(","),
          cover:item[10].split(",")[0],
        }
        shopList.push(shop)
      }
      this.setData({
        shopList:[...this.data.shopList,...shopList],
        last:shopList.length===0
      })
    },
    onPullDownRefresh() {
      const form=this.data.form
      form.searchkey=""
      form.page=0
      form.size=5
      this.setData({
        form
      })
      clearTimeout(this.data.pulldownTimeout)
      const that=this
      this.setData({
        pulldownTimeout:setTimeout(()=>{
          that.setData({
            dynamicList:[]
          })
          that.getshop()
          that.setData({
            pulldownStatus:false
          })
        },800)
      })
    },
    async onReachBottom(){
      if(!this.data.reachButtom&&!this.data.last){
        this.setData({
          reachButtom:true
        })
        const form=this.data.form
        form.page++
        this.setData({
          form
        })
        await this.getshop()
        this.setData({
          reachButtom:false
        })
      }
     
    },
    init(){
      const that=this
      const query = wx.createSelectorQuery().in(this);
      query.select('#scroll-view').boundingClientRect();
      query.exec(function (res) {
      const scrollViewRect = res[0];
      wx.getSystemInfo({
        success:res=>{
        that.setData({
          scrollViewHeight:res.windowHeight
        })
      }
      })
    });
    },
    getDetail(e:any){
      const index=e.currentTarget.dataset.index
      const shop=this.data.shopList[index]
      wx.navigateTo({
        url:`/pages/shop/shop-detail/shop-detail?detail=${JSON.stringify(shop)}`
      })
    }
},
  /**
   * 生命周期函数--监听页面加载
   */
  attached() {
    this.getshop()
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
 async onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
   
  }
})